 package com.nanyang.tms.mq.listener.importforecast;

import java.io.File;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.excel.EasyExcel;
import com.nanyang.common.third.bos.entity.ObsRequestEntity;
import com.nanyang.common.third.bos.util.HwObsUtil;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.service.dataimport.DataImportRpcService;
import com.nanyang.tms.api.service.forecast.ForecastCoreRpcService;
import com.nanyang.tms.common.config.HwObsConfig;
import com.nanyang.tms.mq.listener.message.DataImportMessage;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RocketMQMessageListener(topic = "forecast",selectorExpression ="*", consumerGroup = "nanyang-tms-mq-consumer-import-forecast-group",messageModel = MessageModel.CLUSTERING,consumeMode=ConsumeMode.ORDERLY)
public class ImportForecastListener implements RocketMQListener<MessageExt>{
	@Reference(version = "1.0.0", timeout = 10000, check = false)
	private ForecastCoreRpcService  forecastCoreRpcService;
	@Reference(version = "1.0.0", timeout = 10000, check = false)
	private DataImportRpcService dataImportRpcService;
    @Autowired
	private HwObsConfig hwObsConfig;
	@Override
	public void onMessage(MessageExt message) {
		String body = new String(message.getBody());
		DataImportMessage dataImportMessage = JSONUtil.toBean(body, DataImportMessage.class);
		ObsRequestEntity entity = new ObsRequestEntity();
        BeanUtils.copyProperties(hwObsConfig, entity);
        File resourceFile=null;
		try {
			resourceFile = HwObsUtil.getResourceFile(entity, dataImportMessage.getFileUrl(), hwObsConfig.getLocalFilePath()+HwObsUtil.getObsFileName(dataImportMessage.getFileUrl()));
			EasyExcel.read(resourceFile,ForecastExcelDTO.class,new ForecastReadListener(forecastCoreRpcService, dataImportRpcService, dataImportMessage))
			.sheet()
	        .sheetNo(0)
	        .doRead();
		} catch (Exception e) {
			log.error("预报导入执行失败，参数：{}，错误原因：{}",dataImportMessage.toString(),e.getMessage(),e);
		}finally {
			if(resourceFile!=null){
				resourceFile.delete();
			}
		}
	}
	public static void main(String[] args) {
		 File resourceFile=new File("C:\\Users\\Administrator\\Desktop\\2.xls");
			EasyExcel.read(resourceFile,ForecastExcelDTO.class,new ForecastReadListener(null, null, null))
			.sheet()
	        .sheetNo(0)
	        .doRead();
		
		
	}
      
}
